package cn.com.gridinfo.sooc.core.course.classes.fragment;

import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;

import com.jeremy.arad.utils.Log;
import com.jeremy.arad.widget.dialog.ProgressHUD;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocFragment;
import cn.com.gridinfo.sooc.core.course.classes.activity.CourseDetailActivity;
import cn.com.gridinfo.sooc.core.course.classes.bean.CourseDetailsInfo;
import cn.com.gridinfo.sooc.core.course.classes.dao.CourseDao;
import cn.com.gridinfo.sooc.core.course.common.CodeConst;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 视频 fragment
 */
public class DocFragment extends BaseSoocFragment {

    View view;
    @InjectView(R.id.course_doc)
    WebView courseDocView; //文档播放区域
    @InjectView(R.id.doc_contanier)
    FrameLayout docContanier;//播放区域容器
    private String docPath,type;//文档播放地址

    //课程详情 实体类
    CourseDetailsInfo courseDetailsInfo;

    //课程详情 dao
    CourseDao dao;

    //学习记录ID
    String studyId = "0" ;

    /**
     * 实例化视频Fragment
     *
     * @param path 视频路径
     * @return
     */
    public static DocFragment newInstance( CourseDetailsInfo courseDetailsInfo, String path,String type) {
        DocFragment docFragment = new DocFragment();
        Bundle args = new Bundle();
        args.putString("docPath", path);
        args.putString("type", type);
        args.putSerializable("courseDetail",courseDetailsInfo);
        docFragment.setArguments(args);
        return docFragment;
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.course_doc, null);
        ButterKnife.inject(this, view);
        //课程详情 dao
        dao = new CourseDao(this);
        //文档播放地址
        if (getArguments() != null) {
            docPath = getArguments().getString("docPath");
            type = getArguments().getString("type");
            //获取课程详情
            courseDetailsInfo = (CourseDetailsInfo)getArguments().getSerializable("courseDetail");
        }

        //初始化文档页面
        initDocWebView();

        return view;
    }

    // 初始化文档页面
    private void initDocWebView() {
//        设置文档网页显示，加载url
//        webview 内网页跳转
        courseDocView.setWebViewClient(new WebViewClient(){
            //页面加载前
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
//                showProgress(true,true);//加载进度条、
                Log.i("onPageStarted: "+ url);
                super.onPageStarted(view, url, favicon);

            }

            //页面加载完成
            @Override
            public void onPageFinished(WebView view, String url) {
                Log.i("onPageFinished : "+ url);
                //停止进度条网页加载完成
//                showProgress(false,true);
                super.onPageFinished(view, url);

            }


            //当前加载
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {

//                if(!type.equals("-1")) {
                    view.loadUrl(url);
//                }

                return true;
            }

        });

        courseDocView.setWebChromeClient(new WebChromeClient());
        courseDocView.setClickable(true);
        courseDocView.getSettings().setJavaScriptEnabled(true);//允许脚本
        courseDocView.getSettings().setPluginState(WebSettings.PluginState.ON);
        courseDocView.getSettings().setJavaScriptEnabled(true);
        courseDocView.getSettings().setAllowFileAccess(true);
        courseDocView.getSettings().setDefaultTextEncodingName("GBK");
        courseDocView.getSettings().setAppCacheEnabled(false);
        courseDocView.requestFocus();
        courseDocView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 隐藏缩放图片
        courseDocView.getSettings().setDisplayZoomControls(false);// 显示放大缩小
        String userAgent = courseDocView.getSettings().getUserAgentString();
        if(type.equals("pdf") ) {
            userAgent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us) AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16";
        }
        courseDocView.getSettings().setAllowFileAccess(true);
        courseDocView.getSettings().setUserAgentString(userAgent);
        // 默认缩放模式
        courseDocView.getSettings().setUseWideViewPort(true);// 无限缩放
        courseDocView.getSettings().setLoadWithOverviewMode(true);//加载页面的到屏幕大小模式
//        if(type.equals("pdf")){
//            courseDocView.loadUrl( "http://docs.google.com/gview?embedded=true&url=http://officed02.sooc.site/wv/wordviewerframe.aspx?PdfMode=1&ui=zh-CN&rs=zh-CN&WOPISrc=http%3A%2F%2Fofficed02.sooc.site%2Foh%2Fwopi%2Ffiles%2F%40%2FwFileId%3FwFileId%3Dhttp%3A%2F%2Fftp.sooc.com%2F2016-04-12%2F570c4db6a634b834833772.pdf");//加载文档地址
//        }else {
            courseDocView.loadUrl(docPath);//加载文档地址
//        }
    }


    /**
     * 请求成功,请求成功
     */
    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.status.equals("0")) {
                switch (requestCode) {
                    //记录学习行为
                    case CodeConst.COURSE_SETBEHAVIOR_START:
                        studyId = dao.studyId;//记录学习行为ID
                        break;

                }
            } else {
                MessageUtils.showImageShortToast(getActivity(), dao.statusCode);
            }

        } catch (Exception e) {
//            Log.e(e.getMessage(), e);
//            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    @Override
    public void onResume() {
        super.onResume();

        //记录当前播放内容开始学习行为,并且不是课程老师
        if (courseDetailsInfo.getKcls() == 0) {
            dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), "0",0);
            Log.d("播放记录开始记录..."+courseDetailsInfo.getCurrNrId());
        }

    }

    @Override
    public void onPause() {
        super.onPause();

        //结束学习记录记录学习行为
        if (!studyId.equals("0")) {
            //并且不是课程老师
            if (courseDetailsInfo.getKcls() == 0) {
                dao.setBehavior(courseDetailsInfo.getKcid(), courseDetailsInfo.getCurrPmId(), courseDetailsInfo.getCurrMlId(), courseDetailsInfo.getCurrNrId(), studyId, 0);
                Log.d("播放记录记录停止..."+courseDetailsInfo.getCurrNrId());
            }
        }
    }




    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }
}
